<?php
class Admin_TopicController extends Zend_Controller_Action
{
	public function indexAction()
	{
		// Forward to list action
		$this->_forward('list');
	}
	
	public function listAction()
	{
		$req = $this->getRequest();
		$page = $req->getParam('page', 1);
		$config = My_Globals::getConfiguration();
		$limit = $config->site->admin->limit;		
		$topics = Topic::getInstance()->getTopicList($page, $limit, null, 'topic_id');
		$this->view->topics = $topics;
		$this->view->page = $page;
		$this->view->limit = $limit;
		$this->view->amount = Topic::getInstance()->getAmount();
	}
	
	public function editAction()
	{
		$params = $this->_getAllParams();
		if($params['topic_id'] == 0) return false;
		$topic = Topic::getInstance()->getTopic($params['topic_id']);
		if(!$topic) return false;
		if($this->getRequest()->getMethod() == "POST") {
			$error = false;
			// Clean data
			$params['topic_name'] 		= trim($params['topic_name']);
			$params['topic_title'] 		= trim($params['topic_title']);
			$params['topic_intro'] 		= trim($params['topic_intro']);
			$params['topic_details'] 	= trim($params['topic_details']);
			$params['meta_keywords'] 	= trim($params['meta_keywords']);
			$params['meta_description'] = trim(strip_tags($params['meta_description']));
			// Check data
			$params['topic_alias'] = My_Globals::aliasCreator($params['topic_name']);
			if(Topic::getInstance()->aliasExist($params['topic_alias'], $params['topic_id'])) {
				$error = true;
			}
			if(strlen($params['topic_name']) == 0) {
				$error = true;
			}
			if($params['category_id'] <= 0) {
				$error = true;
			}
			if(strlen($params['topic_intro']) ==0) {
				$error = true;
			} else {
				$params['topic_intro'] = ($params['topic_intro']);
			}
			if(strlen($params['topic_title']) == 0) {
				$params['topic_title'] = $params['topic_name'];
			}
			if(strlen($params['meta_description']) ==0) {
				$params['meta_description'] = trim(strip_tags($params['topic_intro']));
			}
			if($error == false) {
				$image = My_Upload::upload('topic_image', $topic['created']);
				if($image != null) {
					$params['topic_image'] = $image;
				} else {
					$params['topic_image'] = null;
				}
				//print_r($params); exit;
				$params['topic_intro'] = strip_tags($params['topic_intro'], '<a><b><p><strong><br><br /><span><i><u>');
				$params['updated'] = time();
				if(Topic::getInstance()->saveTopic($params)) {
					$this->_redirect($this->view->serverUrl().$this->view->url(array('module'=>'admin', 'controller'=>'topic', 'action'=>'list'), null, true));
					exit;
				}
			}
		}
		$categories = CategoryTopic::getInstance()->getCategoryList();
		$this->view->topic = $topic;
		$this->view->params = $params;
		$this->view->categories = $categories;
	}
	
	public function createAction()
	{
		$params = $this->_getAllParams();
		if($this->getRequest()->getMethod() == "POST") {
			$error = false;
			// Clean data
			$params['topic_name'] 		= trim($params['topic_name']);
			$params['topic_title'] 		= trim($params['topic_title']);
			$params['topic_intro'] 		= trim($params['topic_intro']);
			$params['topic_details'] 	= trim($params['topic_details']);
			$params['meta_keywords'] 	= trim($params['meta_keywords']);
			$params['meta_description'] = trim(strip_tags($params['meta_description']));
			// Check data
			$params['topic_alias'] = My_Globals::aliasCreator($params['topic_title']);
			if(Topic::getInstance()->aliasExist($params['topic_alias'])) {
				$error = true;
			}
			if(strlen($params['topic_name']) == 0) {
				$error = true;
			}
			if($params['category_id'] <= 0) {
				$error = true;
			}
			if(strlen($params['topic_intro']) ==0) {
				$error = true;
			} else {
				$params['topic_intro'] = ($params['topic_intro']);
			}
			if(strlen($params['topic_title']) == 0) {
				$params['topic_title'] = $params['topic_name'];
			}
			if(strlen($params['meta_description']) ==0) {
				$params['meta_description'] = trim(strip_tags($params['topic_intro']));
			}
			if($error == false) {
				$image = My_Upload::upload('topic_image');
				if($image != null) {
					$params['topic_image'] = $image;
				} else {
					$params['topic_image'] = null;
				}
				//print_r($params); exit;
				$params['topic_intro'] = strip_tags($params['topic_intro'], '<a><b><p><strong><br><br /><span><i><u>');
				$params['created'] = $params['updated'] = time();
				if(Topic::getInstance()->saveTopic($params)) {
					$this->_redirect($this->view->serverUrl().$this->view->url(array('module'=>'admin', 'controller'=>'topic', 'action'=>'list'), null, true));
					exit;
				}			}
		}		
		$categories = CategoryTopic::getInstance()->getCategoryList();
		$this->view->params = $params;
		$this->view->categories = $categories;
	}
	
	public function orderAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();		
		foreach($params as $k => $v) {
			$item = str_replace("order_", "", $k);
			if($item > 0 && $v >= 0) {
				$list = array(
					'topic_id' => $item,
					'ordering' => $v
				);
				if(Topic::getInstance()->saveTopic($list)) {
					$this->_returnState(1);
				}
			}
		}
		$this->_returnState(0);
	}
	
	public function deleteAction()
	{
		// Disable template
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$id = (int)$this->getRequest()->getParam('topic_id');
		if( Topic::getInstance()->deleteTopic($id) ) {
			return $this->_returnState(1);
		}
		return $this->_returnState(0);
	}
	
	public function approveAction()
	{
		$this->_helper->layout()->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		$params = $this->_getAllParams();
		if($params['topic_id'] > 0) {
			$list = array(
				'topic_id' => $params['topic_id'],
				'active' => (($params['state'] == 0)?1:0)
			);
			if(Topic::getInstance()->saveTopic($list)) {
				return $this->_returnState(1);
			}
		}
		return $this->_returnState(0);
	}
	
	private function _returnState($state = 1)
	{
		echo json_encode(array('status' => $state));
		exit;
	}
}